package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dao.ServiceAuditRecordMapper;
import com.eastfair.venueservice.entity.ServiceAuditRecord;
import com.eastfair.venueservice.entity.ServiceItem;
import com.eastfair.venueservice.entity.ServiceSpecs;
import com.eastfair.venueservice.enumeration.ServiceItemAuditStateEnum;
import com.eastfair.venueservice.exceptioncode.VenueServiceExceptionCode;
import com.eastfair.venueservice.service.ServiceAuditRecordDetailService;
import com.eastfair.venueservice.service.ServiceAuditRecordService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.venueservice.service.ServiceItemService;
import com.eastfair.venueservice.vo.ServiceAuditRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Objects;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 业务实现类
 * 服务审核记录
 * </p>
 *
 * @author lyw
 * @date 2022-07-13
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ServiceAuditRecordServiceImpl extends SuperServiceImpl<ServiceAuditRecordMapper, ServiceAuditRecord> implements ServiceAuditRecordService {
    @Autowired
    private ServiceItemService serviceItemService;
    @Autowired
    private ServiceAuditRecordService serviceAuditRecordService;
    @Autowired
    private ServiceAuditRecordDetailService serviceAuditRecordDetailService;


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ServiceAuditRecord> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ServiceAuditRecord model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        return R.successDef();
    }


    /**
     * 审核服务
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean auditServiceItemById(Long id, String auditState) {
        log.info("ServiceAuditRecordServiceImpl +++++ auditServiceItemById - 审核服务 params={}, {}", id,auditState);
        ServiceItem serviceItem = serviceItemService.getServiceItemById(id);
        if (Objects.isNull(serviceItem)) {
            throw BizException.wrap(VenueServiceExceptionCode.AUDIT_SERVICE_IS_NULL);
        }
        //审核记录信息
        ServiceAuditRecord serviceAuditRecord = BeanUtil.toBean(serviceItem, ServiceAuditRecord.class);
        serviceAuditRecord.setId(null);
        //审核通过
        if (ServiceItemAuditStateEnum.APPROVED.getCode().equals(auditState)) {
            serviceAuditRecord.setAuditState(ServiceItemAuditStateEnum.APPROVED);
            serviceItemService.updateById(ServiceItem.builder().id(id).auditState(ServiceItemAuditStateEnum.APPROVED).auditBy(ContextUtil.getUserId()).auditTime(LocalDateTime.now()).build());
            // 审核通过后进行同步
            serviceItemService.addServiceItemAfterAudit(id);
        }
        //审核不通过
        if (ServiceItemAuditStateEnum.ON_AUDIT_ADOPT.getCode().equals(auditState)) {
            serviceAuditRecord.setAuditState(ServiceItemAuditStateEnum.ON_AUDIT_ADOPT);
            serviceItemService.updateById(ServiceItem.builder().id(id).auditState(ServiceItemAuditStateEnum.ON_AUDIT_ADOPT).auditBy(ContextUtil.getUserId()).auditTime(LocalDateTime.now()).build());
        }
         serviceAuditRecordService.save(serviceAuditRecord);
        //保存审核详情记录
        serviceAuditRecordDetailService.saveServiceAuditRecordDetail(id,serviceAuditRecord.getId());
        return Boolean.TRUE;
    }
}
